package com.example.lenovo.demo.presenter.adapter;

import android.content.Context;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.lenovo.demo.R;
import com.example.lenovo.demo.model.databean.CityBean;
import com.example.lenovo.demo.model.databean.ModelDaysBean;
import com.example.lenovo.demo.model.idatabean.OnCityClickSuccess;
import com.example.lenovo.demo.view.utils.GvHeightUtil;

import java.util.List;

/**
 * Created by Lenovo on 2017/1/11.
 */

public class CityActivityLvAdapter extends BaseAdapter {
    private List<ModelDaysBean.DataBean.DaysBean> data;
    private String city;
    private Context context;
    private int height=0;
    private OnCityClickSuccess onCityClickSuccess;

    public CityActivityLvAdapter(List<ModelDaysBean.DataBean.DaysBean> data, Context context, String city, OnCityClickSuccess onCityClickSuccess) {
        this.data = data;
        this.context = context;
        this.city=city;
        this.onCityClickSuccess=onCityClickSuccess;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        final ListView listView;
        if(view==null){
            LayoutInflater inflater=LayoutInflater.from(context);
            view= inflater.inflate(R.layout.activity_citylv_item,viewGroup,false);
            listView= (ListView) view.findViewById(R.id.activity_city_lvitem);
            view.setTag(listView);
        }else {
            listView= (ListView) view.getTag();
        }


        ModelDaysBean.DataBean.DaysBean daysBean=data.get(i);
        final CityActivityLvItemLvAdapter adapter=new CityActivityLvItemLvAdapter(daysBean.getPoints(),context,city,daysBean.getDescription(),Integer.toString(i+1),onCityClickSuccess);

        listView.setAdapter(adapter);

        GvHeightUtil.setListViewHeightBasedOnChildren(listView,height);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                LinearLayout layout= (LinearLayout) view.findViewById(R.id.cityac_lvitem_includeLinearLayout);
                ImageView imageView= (ImageView) view.findViewById(R.id.cityhotlv_img);
                listView.setSelectionFromTop(i, 0);
                if(layout!=null&&layout.getVisibility()==View.VISIBLE){
                    layout.setVisibility(View.GONE);
                    imageView.setImageResource(R.drawable.down);
                }else if(layout!=null&&layout.getVisibility()==View.GONE){
                    layout.setVisibility(View.VISIBLE);
                    imageView.setImageResource(R.drawable.up);
                }
            }
        });
        return view;
    }

}
